//
//  HotSpotViewController.m
//  SXH
//
//  Created by Ryan on 15/12/3.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "HotSpotViewController.h"
#import "ADScrollCell.h"
#import "PhotoCompetitionCell.h"
#import "SpotPictureCell.h"
#import "CommonService.h"
#import "UIImageView+AFNetworking.h"
#import "ActivityDetailViewController.h"
#import "ActivityListViewController.h"
#import "PhotoMasterListViewController.h"
#import "WebViewController.h"
#import "FindMeViewController.h"
#import "SDWebImageManager.h"
#define CellType_photo_contest @"photo_contest"             //景区摄影大赛
#define CellType_photographer_teach @"photographer_teach"   //摄影大师教学

@implementation HotSpotViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc]init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.view.backgroundColor = [UIColor colorWithRed:0.886f green:0.886f blue:0.886f alpha:1.00f];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self addHeader];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSString *cellId = nil;
    if (indexPath.row == 0)
    {
        cellId = @"ADScrollCell";
        ADScrollCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil)
        {
        cell = [[ADScrollCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
       
        if (self.mainModel.top_slider.count > 0)
        {
            NSMutableArray *array = [[NSMutableArray alloc]init];
            for (HotSpotModel *model in self.mainModel.top_slider)
            {
                [array addObject:[NSString stringWithFormat:@"%@/%@",IMAGE_URL,model.album_thumb]];
            }
            cell.imageArray = array;
        }
         return  cell;
    }
    else if (indexPath.row == 1||indexPath.row == 2)
    {
        cellId = @"PhotoCompetitionCell";
        PhotoCompetitionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil)
        {
            cell= (PhotoCompetitionCell *)[[[NSBundle  mainBundle]  loadNibNamed:cellId owner:self options:nil]  lastObject];
            cell.delegate = self;
        }
        if(indexPath.row == 1)
        {
            cell.titleLbel.text = @"景区摄影大赛";
            if ([self.mainModel.photo_contest count] == 2)
            {
                HotSpotModel *leftmodel = [self.mainModel.photo_contest firstObject];
                [cell.leftImgView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",IMAGE_URL,leftmodel.album_thumb]]];
                HotSpotModel *rightmodel = [self.mainModel.photo_contest lastObject];
                [cell.rightImgView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",IMAGE_URL,rightmodel.album_thumb]]];
                cell.cellType = CellType_photo_contest;
            }
        }
        else if(indexPath.row == 2)
        {
            cell.titleLbel.text = @"摄影大师教学";
            if ([self.mainModel.photographer_teach count] == 2)
            {
                HotSpotModel *leftmodel = [self.mainModel.photographer_teach firstObject];
                [cell.leftImgView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",IMAGE_URL,leftmodel.album_thumb]]];
                HotSpotModel *rightmodel = [self.mainModel.photographer_teach lastObject];
                [cell.rightImgView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",IMAGE_URL,rightmodel.album_thumb]]];
                cell.cellType = CellType_photographer_teach;
                cell.leftTitle.text = leftmodel.title;
                cell.rightTitle.text = rightmodel.title;
            }
        }
         return  cell;
    }
    else if (indexPath.row == 3)
    {
        cellId = @"SpotPictureCell";
      SpotPictureCell *cell = (SpotPictureCell *)[[[NSBundle  mainBundle]  loadNibNamed:cellId owner:self options:nil]  lastObject];
        if (self.mainModel.travel_list.count == 9)
        {
            [cell spotPictureCellCreateNinePicView:self.mainModel.travel_list];
            cell.delegate = self;
        }
         return  cell;
    }
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 110/320.f*SCREEN_WIDTH;
    }
    else if (indexPath.row == 1||indexPath.row == 2)
    {
        return 140/320.f*SCREEN_WIDTH;
    }
    else if (indexPath.row == 3)
    {
        return 346/320.f*SCREEN_WIDTH;
    }
    return 110/320.f*SCREEN_WIDTH;
}


- (void) getHotSpotDataRefreshView:(MJRefreshBaseView *)refreshView
{
    CommonService *service = [[CommonService alloc] init];
//    [self showLoadingView:YES];
    [service getNetWorkData:nil
                       Path:HotSpot_PATH
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      self.mainModel = [[HotSpotMainModel alloc]initWithDictionary:[dic objectForKey:@"data"] error:nil];
                      [self.tableView reloadData];
//                      [self showLoadingView:NO];
                      [self doneWithView:refreshView];
                  } Failed:^(NSInteger errorCode, NSString *message) {
//                      [self showLoadingView:NO];
                      [self doneWithView:refreshView];
                  }];
}

- (void)selectCompetitionPhoto:(NSInteger)index andType:(NSString *)celltype
{
    NSLog(@"%zi",index);
    if ([celltype isEqualToString:CellType_photo_contest])
    {
        ActivityDetailViewController *mvc = [[ActivityDetailViewController alloc]init];
        HotSpotModel *model = [self.mainModel.photo_contest objectAtIndex:index];
        mvc.spotId = model.id;
        [self.navigationController pushViewController:mvc animated:YES];
    }
    if ([celltype isEqualToString:CellType_photographer_teach])
    {
        WebViewController *mvc = [[WebViewController alloc]init];
        HotSpotModel *model = [self.mainModel.photographer_teach objectAtIndex:index];
        mvc.url = IMAGE_STRING(model.html);
        mvc.vcTitle = model.title;
        [[SDWebImageManager sharedManager]downloadImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",IMAGE_URL,model.album_thumb]] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            mvc.imageShare = image;}];

        mvc.contentShare = model;
        [self.navigationController pushViewController:mvc animated:YES];
    }
}

- (void)NinePicCellClick:(NSInteger)index
{
    NSLog(@"%zi",index);
    HotSpotModel *model = [self.mainModel.travel_list objectAtIndex:index];
    FindMeViewController *mvc = [[FindMeViewController alloc]init];
    mvc.spotModel = model;
    [self.navigationController pushViewController:mvc animated:YES];
}

- (void)selectMoreDetail:(NSString *)cellType
{
    if ([cellType isEqualToString:CellType_photo_contest])
    {
        ActivityListViewController *mvc = [[ActivityListViewController alloc]init];
        [self.navigationController pushViewController:mvc animated:YES];
    }
    if ([cellType isEqualToString:CellType_photographer_teach])
    {
        PhotoMasterListViewController *mvc = [[PhotoMasterListViewController alloc]init];
        [self.navigationController pushViewController:mvc animated:YES];
    }
}



- (void)addHeader
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableView;
    
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        // 进入刷新状态就会回调这个Block
        [self getHotSpotDataRefreshView:refreshView];
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    header.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        // 刷新完毕就会回调这个Block
        NSLog(@"%@----刷新完毕", refreshView.class);
    };
    header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        // 控件的刷新状态切换了就会调用这个block
        switch (state) {
            case MJRefreshStateNormal:
                NSLog(@"%@----切换到：普通状态", refreshView.class);
                break;
            case MJRefreshStatePulling:
                NSLog(@"%@----切换到：松开即可刷新的状态", refreshView.class);
                break;
            case MJRefreshStateRefreshing:
                NSLog(@"%@----切换到：正在刷新状态", refreshView.class);
                break;
            default:
                break;
        }
    };
    [header beginRefreshing];
    _header = header;
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    // 刷新表格
    [self.tableView reloadData];
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}
@end
